<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\tools\Map;
use app\models\Shop;
use app\models\Area;
use app\models\GoodCate;

class ShopController extends BaseController
{
  public function actionShopList(){
    $city_code = Yii::$app->request->post('city_code', 0);
    $city_code = $city_code * 1000000;
    $city_info = Area::find()->where(['area_code' => $city_code])->one();
    //$list = Shop::find()->where(['city_id' => $city_info->id, 'status' => 1])->all();
    $list = Shop::find()->where(['status' => 1])->all();
    $data = [];
    if ($list) {
      foreach($list as $v) {
        $callout = [
          'content' => $v->sub_name,
          'color' => '#FFF',
          'fontSize' => 12,
          'borderRadius' => 5,
          'padding' => 3,
          'bgColor' => '#DA0303',
          'textAlign' => 'center',
          'display' => 'ALWAYS',
          'anchorY' => 66,
        ];
        $data[] = [
          'id' => $v->id,
          'latitude' => $v->lat,
          'longitude' => $v->lng,
          'iconPath' => 'https://img.cyjxy.com/FsDrtaf9Mlfi5rzQ6Z4O1WPspdrL.png',
          'width' => 40,
          'height' => 40,
          'callout' => $callout,
        ];
      }
    }
    return Re::data(['data' => $data]);
  }

  public function actionShopDetail(){
    $id = Yii::$app->request->post('id', 0);
    $lat = Yii::$app->request->post('lat', '');
    $lng = Yii::$app->request->post('lng', '');
    $shop_info = Shop::findOne($id);
    if (!$shop_info) {
      return Re::data(null, '门店信息读取错误', Re::ERROR);
    }
    $data = [
      'id' => $shop_info->id,
      'name' => $shop_info->name,
      'sub_name' => $shop_info->sub_name,
      'logo' => $shop_info->logo,
      'phone' => $shop_info->phone,
      'lng' => $shop_info->lng,
      'lat' => $shop_info->lat,
      'distance' => Map::getDistance($lat, $lng, $shop_info->lat, $shop_info->lng),
      'address' => $shop_info->area->parentArea->parentArea->name.$shop_info->area->parentArea->name.$shop->area->name.$shop->address,
      'service_time' => $shop_info->service_time,
      'cate_list' => GoodCate::find()->where(['status' => 1, 'parent_id' => 0])->select("id, name, icon")->orderBy('sort desc, created_at desc')->all()
    ];
    return Re::data(['data' => $data]);
  }

  public function actionShopListForSelect(){
    $area_code = Yii::$app->request->post('area_code', '');
    $city_code = Yii::$app->request->post('city_code', 0);
    if ($area_code) {
      $area_code = $area_code * 1000000;
      $area_info = Area::find()->where(['area_code' => $area_code])->one();
      $data = Shop::find()->where(['area_id' => $area_info->id, 'status' => 1])->select("id, name, sub_name")->all();
    } else if ($city_code) {
      $city_code = $city_code * 1000000;
      $city_info = Area::find()->where(['area_code' => $city_code])->one();
      $data = Shop::find()->where(['city_id' => $city_info->id, 'status' => 1])->select("id, name, sub_name")->all();
    }
    return Re::data(['data' => $data,]);
  }
}
